import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:${USER}
 * Date:${YEAR}-${MONTH}-${DAY}
 * ${TIME}
 */
public class Main {
     // Definition for singly-linked list.
    public class ListNode {
    int val;
     ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }

    public ListNode swapPairs(ListNode head) {//循环
        if(head==null||head.next==null)return head;
        ListNode newNode=new ListNode(0);
        newNode.next=head;
        ListNode prev=newNode,cur1=prev.next,cur2=cur1.next,curNext=cur2.next;
        while(cur1!=null&&cur2!=null){
            prev.next=cur2;//交换
            cur2.next=cur1;
            cur1.next=curNext;

            prev=cur1;//移动
            cur1=prev.next;
            if(cur1!=null)cur2=cur1.next;
            if(cur2!=null)curNext=cur2.next;
        }
        return newNode.next;
    }
    //递归
    // public ListNode swapPairs(ListNode head) {
    //     if(head==null||head.next==null)return head;
    //     ListNode ret=swapPairs(head.next.next);
    //     ListNode tmp=head.next;
    //     head.next=ret;
    //     tmp.next=head;
    //     return tmp;
    // }

}

